package com.group.view;

import com.group.entity.TimeMove;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Date;
import java.util.TimeZone;

/**
 * @author zhanglianyong
 * 2022/12/23 16:34
 */
public class MainView {

    private JFrame jFrame;
    private JLabel jLabel;
    private TimeMove timeMove;

    public MainView(TimeMove timeMove) {
        this.timeMove = timeMove;
    }

    public void createView() {
        jFrame = new JFrame();
        jFrame.setLocation(400, 300);
        jFrame.setSize(500, 500);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel jPanel = new JPanel();
        jFrame.add(jPanel);
        addComponent(jPanel);
        jFrame.setVisible(true);
    }

    public void addComponent(JPanel panel) {
        panel.setLayout(null);
        panel.setBackground(Color.PINK);
        JLabel time = new JLabel();
        time.setBounds(200, 100,100,50);
        panel.add(time);
        this.setTimer(time);
        jLabel = new JLabel();
        jLabel.setHorizontalAlignment(JLabel.CENTER);
        jLabel.setFont(new Font(Font.SERIF, Font.PLAIN, 14));
        jLabel.setBounds(20, 200,400,50);
        panel.add(jLabel);
        JButton button = new JButton("放慢");
        button.setBackground(Color.YELLOW);
        button.setBounds(100, 300, 100, 20);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                timeMove.setSecond(-2);
                System.out.println("减慢");
            }
        });
        panel.add(button);
        JButton button2 = new JButton("加快");
        button2.setBackground(Color.YELLOW);
        button2.setBounds(250, 300, 100, 20);
        button2.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                timeMove.setSecond(2);
                System.out.println("加快");
            }
        });
        panel.add(button2);
    }

    public void setLabel(String text) {
        this.jLabel.setText(text);
    }

    public void closeWindow() {
        jFrame.setVisible(false);
    }

    /**
     * 1000ms实现一次动作
     * 显示计时
     **/
    private void setTimer(JLabel time) {
        final JLabel varTime = time;
        Timer timeAction = new Timer(1000, new ActionListener() {
            Long timeMillis2 = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
            @Override
            public void actionPerformed(ActionEvent e) {
                long timeline = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
                long timeMilli = timeline - timeMillis2;
                // 转换日期显示格式
                SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
                TimeZone timeZone = TimeZone.getTimeZone("GMT");
                df.setTimeZone(timeZone);
                System.out.println(timeMilli);
                varTime.setText(df.format(new Date(timeMilli)));
            }
        });
        timeAction.start();
    }

}
